package cn.edu.lcu.cs.javateaching.io;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.URLConnection;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.nio.file.attribute.FileAttribute;
import java.time.LocalDate;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.converter.JavaTimeConversionPattern;
import org.junit.jupiter.params.provider.CsvFileSource;
import org.junit.jupiter.params.provider.CsvSource;

import cn.edu.lcu.cs.javateaching.oop.User;

/**
 * NIO2演示
 * 
 * @author Ling
 *
 */
public class Nio2Test {
	@BeforeEach
	public void seperator() {
		System.out.println("\n----------------------华丽的分隔线---------------------------\n");
	}

	@ParameterizedTest
	@CsvSource("doc/java2.jpg, doc/123")
	public void testFileCopy(String src, String dst) {
		try {
			Path source = Paths.get(src);
			Path target = Paths.get(dst);
			if (Files.notExists(target)) {
				Files.createDirectories(target);
			}
			// 将目标文件夹与原始文件名连接，即同名拷贝到目标文件夹
			target = target.resolve(source.getFileName());
			Files.copy(source, target, StandardCopyOption.REPLACE_EXISTING); // 拷贝，指定拷贝选项为替换
			System.out.println("复制成功：" + target.toAbsolutePath());
		} catch (IOException e) {
			System.out.println("复制失败");
			e.printStackTrace();
		}

	}

	/**
	 * 测试Path接口
	 * 
	 * @param src
	 * @param dst
	 */
	@ParameterizedTest
	@CsvSource("doc/1/2/3/4, 5/6")
	public void testPath(String src, String dst) {
		Path source = Paths.get(src);
		Path target = Paths.get(dst);
		System.out.println("source\t" + source);
		System.out.println("target\t" + target);
		
		// 返回路径上最后一个值，不管它是文件还是文件夹
		System.out.println("source.getFileName()\t" + source.getFileName());
		System.out.println("target.getFileName()\t" + target.getFileName());
		
		// 路径表决：如果目标是绝对路径，返回目标路径；如果目标是相对路径，将目标拼接到源路径上
		System.out.println("source.resolve(target)\t" + source.resolve(target));
		
		// 路径表决：用目标路径替换源路径的末端路径，拼接成新路径。目标路径与源路径末端被替换的部分是兄弟
		System.out.println("source.resolveSibling(target)\t" + source.resolveSibling(target));

	}

	/**
	 * 测试Path接口
	 * 
	 * @param src
	 * @param dst
	 * @throws IOException 
	 */
	@ParameterizedTest
	@CsvSource("doc/java2.jpg, 5/6")
	public void testFiles(String src, String dst) throws IOException {
		Path source = Paths.get(src);
		System.out.println(Files.probeContentType(source)); // 获取文档类型
	}

}
